package com.library.utils
{
	import flash.display.GradientType;
	import flash.display.Shape;
	import flash.display.SpreadMethod;
	import flash.geom.Matrix;
	import flash.geom.Point;
	
	public class Geometry
	{
		public function Geometry(){}
		
		public static function DrawCircle(	radius:Number=10, 
											bodyColor:int=-1, 
											bodyAlpha:Number=1, 
											lineColor:int=-1, 
											lineAlpha:Number=1, 
											lineThickness:int=0,
											inCenter:Boolean = false
											):Shape{
			var shapeCircle:Shape = new Shape();
			if(bodyColor != -1){
				shapeCircle.graphics.beginFill(bodyColor,bodyAlpha);
			}
			if(lineColor != -1){
				shapeCircle.graphics.lineStyle(lineThickness, lineColor,lineAlpha);
			}
			var x:Number = 0;
			var y:Number = 0;
			
			if(!inCenter){
				x = radius*2;
				y = radius*2;
			}
			shapeCircle.graphics.drawCircle(x, y,  radius);
			return shapeCircle;
		}
		
		public static function DrawArc(
											shapeArc:Shape,
											nRadius:Number, 
											nArc:Number,
											nStartingAngle:Number = 0,
											inCenter:Boolean = false,
											bRadialLines:Boolean = false):void
		{
			var x:Number = 0;
			var y:Number = 0;
			
			if(!inCenter){
				x = nRadius*2;
				y = nRadius*2;
			}
			
			// The angle of each of the eight segments is 45 degrees (360 divided by eight),
			// which equals p/4 radians.
			if(nArc > 360) nArc = 360;
	
			nArc = Math.PI/180 * nArc;
			var nAngleDelta:Number = nArc/8;
			
			// Find the distance from the circle's center to the control points
			// for the curves.
			var nCtrlDist:Number = nRadius/Math.cos(nAngleDelta/2);
			
			nStartingAngle *= Math.PI / 180;
			
			var nAngle:Number = nStartingAngle;
			var nCtrlX:Number;
			var nCtrlY:Number;
			var nAnchorX:Number;
			var nAnchorY:Number;
			
			var nStartingX:Number = x + Math.cos(nStartingAngle) * nRadius;
			var nStartingY:Number = y + Math.sin(nStartingAngle) * nRadius;
			
			if(bRadialLines){
				shapeArc.graphics.moveTo(x, y);
				shapeArc.graphics.lineTo(nStartingX, nStartingY);
			}else {
				// Move to the starting point, one radius to the right of the circle's center.
				shapeArc.graphics.moveTo(nStartingX, nStartingY);
			}
			
			
			// Repeat eight times to create eight segments.
			for (var i:Number = 0; i < 8; i++){
			
				// Increment the angle by angleDelta (p/4) to create the whole circle (2p).
				nAngle += nAngleDelta;
				
				// The control points are derived using sine and cosine.
				nCtrlX = x + Math.cos(nAngle-(nAngleDelta/2))*(nCtrlDist);
				nCtrlY = y + Math.sin(nAngle-(nAngleDelta/2))*(nCtrlDist);
				
				// The anchor points (end points of the curve) can be found similarly to the
				// control points.
				nAnchorX = x + Math.cos(nAngle) * nRadius;
				nAnchorY = y + Math.sin(nAngle) * nRadius;
				
				// Draw the segment.
				shapeArc.graphics.curveTo(nCtrlX, nCtrlY, nAnchorX, nAnchorY);
			}
			
			if(bRadialLines){
				shapeArc.graphics.lineTo(x, y);
			}			
		}
		
		public static function DrawArc1 ( 
											nRadius:Number, 
											nArc:Number, 
											nStartingAngle:Number = 0, 
											bodyColor:int=-1, 
											bodyAlpha:Number=1, 
											lineColor:int=-1, 
											lineAlpha:Number=1, 
											lineThickness:int=0,
											inCenter:Boolean = true,
											bRadialLines:Boolean = false):Shape{
			
			var shapeArc:Shape = new Shape();
			
			if(bodyColor != -1){
				shapeArc.graphics.beginFill(bodyColor,bodyAlpha);
			}
			if(lineColor != -1){
				shapeArc.graphics.lineStyle(lineThickness, lineColor,lineAlpha);
			}
			DrawArc(shapeArc, nRadius, nArc, nStartingAngle, inCenter, bRadialLines);
			
			return shapeArc;
		}
		
		public static function DrawRect(	width:Number=10, 
											height:Number=10, 
											bodyColor:int=-1, 
											bodyAlpha:Number=1, 
											lineColor:int=-1, 
											lineAlpha:Number=1, 
											lineThickness:int=0,
											ellipseWidth:Number=0,
											ellipseHeight:Number=0,
											inCenter:Boolean = false
											):Shape{
			var shapeRect:Shape = new Shape();
			if(bodyColor != -1){
				shapeRect.graphics.beginFill(bodyColor,bodyAlpha);
			}
			if(lineColor != -1){
				shapeRect.graphics.lineStyle(lineThickness, lineColor,lineAlpha);
			}
			var x:Number = 0;
			var y:Number = 0;
			if(inCenter){
				x = -width/2;
				y = -height/2;
			}
			if(ellipseHeight != 0 && ellipseWidth != 0){
				shapeRect.graphics.drawRoundRect(x, y, width, height, ellipseWidth, ellipseHeight);
			}else{
				shapeRect.graphics.drawRect(x, y, width, height);
			}
			return shapeRect;
		}
		
		public static function DrawTriangle(	long:Number=10, 
												bodyColor:int=-1, 
												bodyAlpha:Number=1, 
												lineColor:int=-1, 
												lineAlpha:Number=1, 
												lineThickness:int=0):Shape{
        	var triangle:Shape = new Shape();
        	if(bodyColor != -1){
				triangle.graphics.beginFill(bodyColor,bodyAlpha);
			}
			if(lineColor != -1){
				triangle.graphics.lineStyle(lineThickness, lineColor,lineAlpha);
			}
        	// 输入两边一角画三角形
            var point1:Point = new Point();
            point1.x = -long/2;
            point1.y = Math.tan(30/180*Math.PI) * long/2;
            
            var point2:Point = new Point();
            point2.x = 0;
            point2.y = -point1.y*2;
            
            var point3:Point = new Point();
            point3.x = long/2;
            point3.y = point1.y;
            
            
			var a:Number = 10;
			var b:Number = 10;
			var ag:Number = 60;
			triangle.graphics.moveTo(point1.x,point1.y);
			triangle.graphics.lineTo(point2.x,point2.y);
			triangle.graphics.lineTo(point3.x,point3.y);
			triangle.graphics.lineTo(point1.x,point1.y);
			
			return triangle;
        }
        
        public static function DrawGradientBox(width:Number, height:Number, color1:uint, color2:uint):Shape{
        	var gBox:Shape = new Shape();
        	
        	var fillType:String = GradientType.LINEAR;
			var colors:Array = [color1, color2];
			var alphas:Array = [100, 100];
			var ratios:Array = [0x00, 0xFF];
			var matr:Matrix = new Matrix();
			matr.createGradientBox(width, height, 0, 0, 0);
			var spreadMethod:String = SpreadMethod.PAD;
			gBox.graphics.beginGradientFill(fillType, colors, alphas, ratios, matr, spreadMethod);
			gBox.graphics.drawRect(0,0,width,height);
			
			return gBox;
        }
        
        // 没有左右边线，只有上下边线
        public static function DrawRect2(	width:Number=10, 
											height:Number=10, 
											bodyColor:int=-1, 
											bodyAlpha:Number=1, 
											lineColor:int=-1, 
											lineAlpha:Number=1, 
											lineThickness:int=0,
											ellipseWidth:Number=0,
											ellipseHeight:Number=0,
											inCenter:Boolean = false
											):Shape{
			var shapeRect:Shape = new Shape();
			if(bodyColor != -1){
				shapeRect.graphics.beginFill(bodyColor,bodyAlpha);
			}
			if(ellipseHeight != 0 && ellipseWidth != 0){
				shapeRect.graphics.drawRoundRect(x, y, width, height, ellipseWidth, ellipseHeight);
			}else{
				shapeRect.graphics.drawRect(x, y, width, height);
			}
			var x:Number = 0;
			var y:Number = 0;
			if(inCenter){
				x = -width/2;
				y = -height/2;
				
				if(lineColor != -1){
					shapeRect.graphics.lineStyle(lineThickness, lineColor,lineAlpha);
					
					shapeRect.graphics.moveTo(-width/2, -height/2);
					shapeRect.graphics.lineTo(width/2, -height/2);
					shapeRect.graphics.moveTo(-width/2, height/2);
					shapeRect.graphics.lineTo(width/2, height/2);
				}
			}else{
				if(lineColor != -1){
					shapeRect.graphics.lineStyle(lineThickness, lineColor,lineAlpha);					
					shapeRect.graphics.moveTo(0, 0);
					shapeRect.graphics.lineTo(width, 0);
					shapeRect.graphics.moveTo(0, height);
					shapeRect.graphics.lineTo(width, height);
				}
			}
			
			
			return shapeRect;						
		}
	}
}